package cn.kz.hrm.service.impl;

import cn.kz.hrm.commonfeiclient.CommonFeignClient;
import cn.kz.hrm.domain.Sso;
import cn.kz.hrm.domain.VipBase;
import cn.kz.hrm.dto.RegisterDto;
import cn.kz.hrm.mapper.SsoMapper;
import cn.kz.hrm.mapper.VipBaseMapper;
import cn.kz.hrm.service.ISsoService;
import cn.kz.hrm.util.AjaxResult;
import cn.kz.hrm.util.encrypt.MD5;
import cn.kz.hrm.vo.SMSCodeSendVo;
import cn.kz.krm.myexceptions.MyGlobalExceptions;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author kz
 * @since 2020-07-05
 */
@Service
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {

    @Autowired
    private CommonFeignClient commonFeignClient;

    @Autowired
    private VipBaseMapper vipBaseMapper;

    /**
     * 用户点击注册按钮
     * @param registerDto
     */
    @Override
    public void phoneRegister(RegisterDto registerDto) {
        //手机号
        String mobile = registerDto.getMobile();
        //密码
        String password = registerDto.getPassword();
        //验证码用户输入的值，做匹配与redis是否一致！
        String smsCode = registerDto.getSmsCode();
//1. todo 基本参数校验
        if(mobile == null){
            throw new MyGlobalExceptions("手机号不能为空，请输入正确的手机号！");
        }
        if(password == null){
            throw new MyGlobalExceptions("密码不能为空，请设置密码！！");
        }
        if(smsCode == null){
            throw new MyGlobalExceptions("验证码不能为空，请输入正确的验证码！！");
        }
//2. todo 手机验证码校验
        Sso ssoFromMysql =  baseMapper.getByMobile(mobile);
        if(ssoFromMysql != null){//能根据手机号查询到数据
            throw new MyGlobalExceptions("该手机号已经被注册！");
        }
//3. todo 判断手机验证码
        //根据key去获取value值
        String codeToRedisKey = VerifycodeServiceImpl.PHONE_REGISTER_CODE_REDIS + mobile;
        //后去value值
        AjaxResult ajaxResult = commonFeignClient.getRedis(codeToRedisKey);
        //判断是否过期，或者是否输入错误
        if(ajaxResult.getResultObj() == null || !ajaxResult.isSuccess()){
            throw new MyGlobalExceptions("验证码已过期，或无效，请重新获取验证码！！");
        }
        //将验证按解析成SMSCodeSendVo对象
        //将value值进行转换成SMSCodeSendVo对象，该类中封装了value中参数
        String valueToRedis = ajaxResult.getResultObj().toString();
        SMSCodeSendVo smsCodeSendVo = JSONObject.parseObject(valueToRedis, SMSCodeSendVo.class);
        if(!smsCodeSendVo.getCode().equals(smsCode.trim())){
            throw new MyGlobalExceptions("验证码匹配失败！");
        }

        //4.todo  保存SSO注册信息 到达这里就证明是所有数据都匹配且数据样式合格
        long time = new Date().getTime();
        Sso sso = new Sso();
        sso.setPhone(mobile);
        //对密码进行加密设置，加密加盐
        String salt = UUID.randomUUID().toString();
        String pwd = MD5.getMD5(password+salt);
        sso.setPassword(pwd);//存储密码
        sso.setSalt(salt);//存储盐值
        sso.setCreateTime(time);
        baseMapper.insert(sso);

        //5.todo  保存VIP_Base基本信息
        VipBase vipBase = new VipBase();
        //t_sso和t_vip_base关联表ssoId是sso的外键
        vipBase.setSsoId(sso.getId());
        //创建时间
        vipBase.setCreateTime(time);
        vipBase.setRegChannel(1);  //1 默认为pc端登录
        //注册时间
        vipBase.setRegTime(time);
        vipBaseMapper.insert(vipBase);
    }
}
